package demo5;

/**
 * Created with IntelliJ IEDA.
 * Description:
 * User:86186
 * Date:2022-05-20
 * Time:23:29
 */
class Money implements Cloneable{
    public double money = 19.9;

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();//重写克隆方法
    }
}

class Person implements Cloneable{
    public int age = 10;
    public Money m = new Money();

    @Override
    protected Object clone() throws CloneNotSupportedException {
        Person temp = (Person)super.clone();
        temp.m = (Money)this.m.clone();
        return temp;
        //return super.clone();//要克隆必须要重写一个克隆方法
    }

    @Override
    public String toString() {
        return "person{" +
                "age=" + age +
                '}';

    }
}

public class Test {
    public static void main(String[] args) throws CloneNotSupportedException {
        Person person = new Person();
        Person person2 = (Person) person.clone();
        System.out.println(person.m.money);
        System.out.println(person2.m.money);
        System.out.println("=========================");
        person2.m.money = 99.9;
        System.out.println(person.m.money);
        System.out.println(person2.m.money);
    }
}
